/*!
 * \file Hash.h
 * \brief Hash value generator.
 * \author Nus
 * \date 2008/02/15, 23:49
 */

#ifndef __HASH_H__
#define __HASH_H__

#include "Types.h"
/*!
 * \class HashValue
 * \brief Hash value & hash value generator.
 */
class HashValue
{
public:
  //! \brief Default constructor.
  HashValue(const s8* str = NULL)
      : mValue(0)
  {
    if(str)
      calcHashValue(str, strlen(str));
  }
  
  //! \brief Constructor.
  HashValue(const s8* str, u32 length)
      : mValue(0)
  {
    calcHashValue(str, length);
  }
  
  //! \brief Constructor.
  HashValue(const HashValue& val)
      : mValue(val.mValue)
  {
    // None...
  }
  
  //! \brief Default destructor.
  ~HashValue() {
    // None...
  }
  
  //! \brief Assign operator.
  HashValue& operator = (const HashValue& val) {
    mValue = val.mValue;
    return *this;
  }
  
  //! \brief Assign operator.
  HashValue& operator = (const s8* str) {
    if(str)
      calcHashValue(str, strlen(str));
    else
      mValue = 0;
    return *this;
  }

  //! \brief Operator equal.
  bool operator == (const HashValue& val) const {
    return mValue == val.mValue;
  }

  //! \brief Operator greater.
  bool operator > (const HashValue& val) const {
    return mValue > val.mValue;
  }

  //! \brief Operator less.
  bool operator < (const HashValue& val) const {
    return mValue < val.mValue;
  }

  //! \brief Operator greater equal.
  bool operator >= (const HashValue& val) const {
    return mValue >= val.mValue;
  }

  //! \brief Operator less equal.
  bool operator <= (const HashValue& val) const {
    return mValue < val.mValue;
  }
  
  //! \brief Cast to unsigned int.
  operator u32 (void) const {
    return mValue;
  }

protected:
  // None...

private:
  u32 mValue;                     //!< Hash value.

  //! \brief Generate hash value.
  //! \note Based on SuperFastHash by Paul Hsieh.
  //! http://www.azillionmonkeys.com/qed/hash.html
  u32 calcHashValue(const s8* str, u32 length);

};

#endif
